package com.ruoyi.web.controller.stuhelp;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stuhelp.domain.StudentsHelp;
import com.ruoyi.stuhelp.domain.vo.StudentCategoryHelpVo;
import com.ruoyi.stuhelp.service.IStudentsHelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 校园公益Controller
 * 
 * @author atzxc
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/stuhelp/stuhelp")
public class StudentsHelpController extends BaseController
{
    @Autowired
    private IStudentsHelpService studentsHelpService;

    /**
     * 查询校园公益列表
     */
    @PreAuthorize("@ss.hasPermi('stuhelp:stuhelp:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentCategoryHelpVo studentCategoryHelpVo)
    {
        startPage();
        List<StudentCategoryHelpVo> list = studentsHelpService.selectStudentsAndCategoryHelpList(studentCategoryHelpVo);
        return getDataTable(list);
    }

    /**
     * 导出校园公益列表
     */
    @PreAuthorize("@ss.hasPermi('stuhelp:stuhelp:export')")
    @Log(title = "校园公益", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentsHelp studentsHelp)
    {
        List<StudentsHelp> list = studentsHelpService.selectStudentsHelpList(studentsHelp);
        ExcelUtil<StudentsHelp> util = new ExcelUtil<StudentsHelp>(StudentsHelp.class);
        util.exportExcel(response, list, "校园公益数据");
    }

    /**
     * 获取校园公益详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuhelp:stuhelp:query')")
    @GetMapping(value = "/{shelpId}")
    public AjaxResult getInfo(@PathVariable("shelpId") Long shelpId)
    {
        StudentCategoryHelpVo studentCategoryHelpVo = studentsHelpService.selectStudentsHelpByShelpId(shelpId);
        System.out.println(studentCategoryHelpVo);
        return success(studentCategoryHelpVo);
    }

    /**
     * 新增校园公益
     */
    @PreAuthorize("@ss.hasPermi('stuhelp:stuhelp:add')")
    @Log(title = "校园公益", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentsHelp studentsHelp)
    {
        return toAjax(studentsHelpService.insertStudentsHelp(studentsHelp));
    }

    /**
     * 修改校园公益
     */
    @PreAuthorize("@ss.hasPermi('stuhelp:stuhelp:edit')")
    @Log(title = "校园公益", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentsHelp studentsHelp)
    {

        String imageURL = studentsHelp.getImageURL();
        String[] split = imageURL.replaceAll("[\\[\\]\"]","").trim().split(",");
        System.out.println("处理后的数据为："+split.toString());
        studentsHelp.setImageURL(JSON.toJSONString(split));
        System.out.println("前台获取到的数据："+studentsHelp);
        return toAjax(studentsHelpService.updateStudentsHelp(studentsHelp));
    }

    /**
     * 删除校园公益
     */
    @PreAuthorize("@ss.hasPermi('stuhelp:stuhelp:remove')")
    @Log(title = "校园公益", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shelpIds}")
    public AjaxResult remove(@PathVariable Long[] shelpIds)
    {
        return toAjax(studentsHelpService.deleteStudentsHelpByShelpIds(shelpIds));
    }
}
